package org.example;

import java.io.IOException;
import java.util.Properties;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;



public class Main {

    public Object getFromPro(String path) {//读取属性文件
        Properties properties = new Properties();
        String className;
        try (InputStream proInput = this.getClass().getResourceAsStream(path)) {
            if (proInput == null) {
                return null;
            }
            properties.load(proInput);
            className = properties.getProperty("bootstrapClass");
            System.out.println(className);
        } catch (IOException e) {
            System.out.println("载入出错");
            return null;
        }
        if (className != null) {
            try {
                Class<?> newClass = Class.forName(className);
                Constructor<?> co = newClass.getConstructor();
                return co.newInstance();
            } catch (NoSuchMethodException e) {
                System.out.println("未找到相关方法");
                return null;
            } catch (ClassNotFoundException e) {
                System.out.println("未找到相关类");
                return null;
            } catch (InstantiationException e) {
                System.out.println("实例化出错");
                return null;
            } catch (IllegalAccessException e) {
                System.out.println("非法引用方法");
                return null;
            } catch (InvocationTargetException e) {
                System.out.println("调用异常");
                return null;
            }
        } else {
            return null;
        }
}
//调用带有 @InitMethod 注解的方法
    public boolean getMethod(Object object, String args) {
        if (object != null) {
            Class<?> newClass = object.getClass();
            Method[] methods = newClass.getDeclaredMethods();
            for (Method method : methods) {
                Annotation[] declaredAnnotations = method.getDeclaredAnnotations();
                for (Annotation annotation : declaredAnnotations) {
                    if ("@org.example.InitMethod()".equals(annotation.toString())) {
                        try {
                            if (args != null) {
                                method.invoke(object, args);
                                return true;
                            } else {
                                method.invoke(object);
                                return true;
                            }
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else {return false;}
                }
            }
            return false;
        }
        else {return false;}
    }
    public static void main(String[] args) {
        Main toCreate = new Main();
        String path = "/myapp.properties";
        Object obj = toCreate.getFromPro(path);
        toCreate.getMethod(obj, null);
    }
}
